using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DepartmentManager = CentralOffice.Admin.BusinessActions.Department;
using DepartmentTable = CentralOffice.Admin.Data.Departments.DepartmentsDataTable;
using DepartmentRow = CentralOffice.Admin.Data.Departments.DepartmentsRow;


namespace CentralOffice.WebClient.Admin.Department
{
    public partial class DepartmentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DepartmentTable depts = DepartmentManager.GetTopDepartment();

                foreach (DepartmentRow row in depts.Rows)
                {
                    if (row.IsDeleted || !row.IsActive) continue;
                    TreeNode node = new TreeNode();
                    node.Text = "[" + row.Name + "]";
                    node.Value = row.Id.ToString();
                    node.SelectAction = TreeNodeSelectAction.Expand;
                    //node.ImageUrl = "../../images/menu/system.gif";
                    node.ImageUrl = "~/images/menu/system.gif";
                    //node.ToolTip = row.DepartmentNumber;
                    //node.PopulateOnDemand = true;
                    //node.SelectAction = TreeNodeSelectAction.Select;

                    tvDept.Nodes.Add(node);
                    node.Expanded = true;

                    AddDepartment(node);
                }
            }
        }

        private void AddDepartment(TreeNode node)
        {
            Guid id = new Guid(node.Value);
            DepartmentTable depts = DepartmentManager.GetSubDepartment(id);

            foreach (DepartmentRow dept in depts)
            {
                if (dept.IsDeleted || !dept.IsActive) continue;
                TreeNode newNode = new TreeNode();
                newNode.Text = "[" + dept.Name + "]"; //dept.Name;
                newNode.Value = dept.Id.ToString();
                newNode.Target = "dept_main";
                //newNode.ImageUrl = "../../images/node_dept.gif";
                newNode.ImageUrl = "~/images/node_dept.gif";
                newNode.NavigateUrl = "DepartmentEdit.aspx?deptid=" + dept.Id.ToString() + "&parentid=" + node.Value;
                
                newNode.Expanded = false;
                node.ChildNodes.Add(newNode);

                AddDepartment(newNode);
            }
        }

        //protected void tvDept_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        //{
        //    if (this.IsCallback)
        //    {
        //        if (e.Node.ChildNodes.Count == 0)
        //        {
        //            LoadChildNode(e.Node);
        //        }
        //    }
        //}

        //private void LoadChildNode(TreeNode node)
        //{
        //    DepartmentTable depts = DepartmentManager.GetSubDepartment(new Guid(node.Value));

        //    foreach (DepartmentRow row in depts)
        //    {

        //        TreeNode subNode = new TreeNode(row.Name);
        //        subNode.Value = row.Id.ToString();

        //        try
        //        {
        //            if (!row.IsLeaf)//DepartmentManager.GetSubDepartment(row.Id).Rows.Count > 0
        //            {
        //                subNode.SelectAction = TreeNodeSelectAction.SelectExpand;
        //                subNode.PopulateOnDemand = true;
        //                subNode.NavigateUrl = "#";
        //            }
        //        }
        //        catch
        //        {
        //            subNode.ImageUrl = "WebResource.axd?a=s&r=TreeView_XP_Explorer_ParentNode.gif&t=632242003305625000";
        //        }
        //        node.ChildNodes.Add(subNode);

        //        if (subNode.Depth == 0)
        //        {
        //            subNode.Expand();
        //        }

        //    }
        //}

    }
}
